package com.java2007.hotel.controller.user;

import com.java2007.hotel.constant.ServletConstant;
import com.java2007.hotel.controller.Controller;
import com.java2007.hotel.pojo.User;
import com.java2007.hotel.service.UserService;
import com.java2007.hotel.service.impl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.Servlet;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author Reckeys
 * @version V1.0
 * @date 2021/01/25
 **/
@WebServlet("/user")
public class UserController extends Controller {
    private UserService userService = new UserServiceImpl();

    /**
     * 用户登陆
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String userLogin(HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            //获取输入的验证码
            String validatecode = request.getParameter("validate");
            //获取系统生成的验证码
            String code = (String)request.getSession().getAttribute("code");
            //判断验证码是否一致（不区分大小写）
            if (validatecode != null && validatecode.equalsIgnoreCase(code.trim())) {

                //获取输入的用户名和密码
                String userName = request.getParameter("userName");
                String userPassword = request.getParameter("userPassword");
                //获取对应的用户
                User user = userService.userLogin(userName, userPassword);
                //登录的用户信息保存到session

                //判断用户状态
                if (0 == user.getUserStatus()){
                    //未激活
                    return ServletConstant.jump("用户被封禁","redirect:/user/login.jsp");
                }else if (1 == user.getUserStatus()){

                    //激活，判断用户权限
                    if(0 == user.getUserRole() || 1 == user.getUserRole()){
                        //管理员用户
                        request.getSession().setAttribute("user", user);
                        return "redirect:/backend/index.jsp";
                    }if(2 == user.getUserRole()){
                        //vip用户
                        request.getSession().setAttribute("user", user);
                        return "redirect:/front?method=findDinnerTableByStatus";
                    }
                    else {
                        //普通用户
                        request.getSession().setAttribute("user", user);
                        return "redirect:/front?method=findDinnerTableByStatus";
                    }
                }
                return "用户或密码错误错误！";
            }
            //登陆失败
            return "验证码错误！";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("loginFailMsg", e.getMessage());
            return "redirect:/user/login.jsp";
        }

    }

    /**
     * 用户注册
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
   public String userRegistere(HttpServletRequest request, HttpServletResponse response) throws Exception {
       Map<String, String[]> parameterMap = request.getParameterMap();
       User user = new User();
       BeanUtils.populate(user,parameterMap);
       String message = userService.userRegistere(user);
       return ServletConstant.jump(message,"user/registere.jsp");
   }

    /**
     * 用户退出
     * 删除session域内所有数据
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String userLogout(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.getSession().removeAttribute("user");
        HttpSession session = request.getSession();
        session.removeAttribute("order");
        session.removeAttribute("totalPrice");
        session.removeAttribute("orderDetailsList");
        session.removeAttribute("tableId");
        return "redirect:/user/login.jsp";
    }

    /**
     * 注册时判断用户名是否存在
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String findUserReChecking(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String userName = request.getParameter("userName");

        //判断用户是否存在，0：不存在 1：存在
        int userreChecking = userService.findUserReChecking(userName);
        if (userreChecking == 1){
            //存在返回false
            return "false";
        }else {
            //不存在返回success
            return "success";
        }

    }


}